<?php
class BaseModel extends CActiveRecord
{
   	/*public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }*/

    public function behaviors() {
        return array(
            'ERememberFiltersBehavior' => array(
                'class' => 'application.behaviors.ERememberFiltersBehavior',
                'defaults'=>array(),           /* optional line */
                'defaultStickOnClear'=>true   /* optional line */
            ),
        );
    }

    protected function beforeFind() {
        $this->cache(Yii::app()->params['cacheTime'], new CTagCacheDependency(get_class($this), 'cache'));
        parent::beforeFind();
    }
    protected function afterDelete()
    {
        parent::afterDelete();
        Yii::app()->cache->set(get_class($this), microtime(true), 0);
    }
    protected function afterSave()
    {
        parent::afterSave();
        Yii::app()->cache->set(get_class($this), microtime(true), 0);
    }
    protected function beforeSave()
    {
    	if(isset($_POST[ucfirst(Yii::app()->controller->getId())]['alias'])){
    		$alias = $_POST[ucfirst(Yii::app()->controller->getId())]['alias'];
    		$controller = ucfirst(Yii::app()->controller->getId());
    		$obj = $controller::model();
    		$crnt_alias = $obj->findByPk(Yii::app()->request->getParam('id'));
    		$remove = '';
    		if(isset($_POST[ucfirst(Yii::app()->controller->getId())]['category_id'])){
	    		$parentObj = 'Category'.$controller;
	    		$parent_alias = $parentObj::model()->findByPk($_POST[ucfirst(Yii::app()->controller->getId())]['category_id']);
	    		$alias = isset($parent_alias->url) ? $parent_alias->url->alias.'/'.$alias : $parent_alias->alias.'/'.$alias;
    		}
    		if(isset($_POST[ucfirst(Yii::app()->controller->getId())]['parent_id'])){
    			$parentObj = $controller;
    			$parent_alias = $parentObj::model()->findByPk($_POST[ucfirst(Yii::app()->controller->getId())]['parent_id']);
    			if($parent_alias){
    				$alias = isset($parent_alias->url) ? $parent_alias->url->alias.'/'.$alias : $parent_alias->alias.'/'.$alias;
    			}
    		}
    		if(isset($crnt_alias->url_id)) {
    			Urls::model()->deleteByPk($crnt_alias->url_id);
    		}
    		$model = new Urls;
    		$model->route = $obj->route;
    		$model->get_param = $obj->get_param;
    		$model->alias = $alias;
    		if($model->validate()){
    			$model->save();
                //$this->url_id = $model->getPrimaryKey();
    			if(isset($this->url_id)) $this->url_id = $model->getPrimaryKey();
    		}
    	}
    	parent::beforeSave();
    	return true;
    }
}
?>